package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Member;
import com.embhealth.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 会员档案管理
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.findPage(queryPageBean);
    }
    //member批量删除
    @RequestMapping("/del_member")
    public Result del_member(@RequestBody Map map){

        //System.out.println(jsonstring);
        try {
//            解析json
//            JSONObject jsonObject = new JSONObject(Boolean.parseBoolean(jsonstring));
//            JSONArray ids = jsonObject.getJSONArray("ids");
//            System.out.println("默默"+ids.get(0));
            memberService.del_member(map);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    //添加数据
    @RequestMapping("/add_member")
    public Result add_member(@RequestBody Member member){
        try {
            memberService.add_member(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }

    }
    //编辑
    @RequestMapping("/edit_member")
    public Result edit_member(@RequestBody  Member member){
        try{
            memberService.edit_member(member);//发送请求
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
    //根据id查询检查项
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Member member = memberService.findById(id);//发送请求
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    //根据id删除检查项
    @RequestMapping("/delete")
    //看看是否具有permission表角色权限
    public Result delete(Integer id){
        try{
            memberService.deleteById(id);//发送请求
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }










}
